package com.ruoyi.project.men.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.men.domain.MenContactUsDO;
import com.ruoyi.project.men.service.MenContactUsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 联系我们Controller
 *
 * @author long
 * @date 2021-04-15
 */
@RestController
@RequestMapping("/men/menContactUs")
public class MenContactUsController extends BaseController
{
	@Autowired
	private MenContactUsService menContactUsService;

	/**
	 * 查询联系我们列表
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('men:menContactUs:list')")
	@GetMapping("/list")
	public TableDataInfo list(MenContactUsDO menContactUs)
	{
		startPage();
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(menContactUs);
		stringObjectMap.put("delFlag","0");
		stringObjectMap.put("sort","ca_at");
		stringObjectMap.put("order","desc");
		List<MenContactUsDO> list = menContactUsService.list(stringObjectMap);
		return getDataTable(list);
	}

	/**
	 * 导出联系我们列表
	 */
	@PreAuthorize("@ss.hasPermi('men:menContactUs:export')")
	@Log(title = "联系我们", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(MenContactUsDO menContactUs)
	{
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(menContactUs);
		List<MenContactUsDO> list = menContactUsService.list(stringObjectMap);
		ExcelUtil<MenContactUsDO> util = new ExcelUtil<MenContactUsDO>(MenContactUsDO.class);
		return util.exportExcel(list, "menContactUs");
	}

	/**
	 * 获取联系我们详细信息
	 */
	@PreAuthorize("@ss.hasPermi('men:menContactUs:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") String id)
	{
		return AjaxResult.success(menContactUsService.get(id));
	}

	/**
	 * 新增联系我们
	 */
	@PreAuthorize("@ss.hasPermi('men:menContactUs:add')")
	@Log(title = "联系我们", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody MenContactUsDO menContactUs)
	{
		menContactUs.setId(IdUtil.simpleUUID());
		menContactUs.setDelFlag(0);
		menContactUs.setCaAt(new Date());
		return toAjax(menContactUsService.save(menContactUs));
	}

	/**
	 * 修改联系我们
	 */
	@PreAuthorize("@ss.hasPermi('men:menContactUs:edit')")
	@Log(title = "联系我们", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody MenContactUsDO menContactUs)
	{
		menContactUs.setUpAt(new Date());
		return toAjax(menContactUsService.update(menContactUs));
	}

	/**
	 * 删除联系我们
	 */
	@PreAuthorize("@ss.hasPermi('men:menContactUs:remove')")
	@Log(title = "联系我们", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable String[] ids)
	{
		if(ids.length>0){
			for (String string : ids) {
				MenContactUsDO menContactUs = menContactUsService.get(string);
				menContactUs.setDelFlag(1);
				menContactUs.setUpAt(new Date());
				menContactUsService.update(menContactUs);
			}
		}
		return toAjax(ids.length);
		//return toAjax(menContactUsService.deleteMenContactUsByIds(ids));
	}
}
